package converters;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import  javax.faces.component.UIComponent;
import  javax.faces.context.FacesContext;
import  javax.faces.convert.Converter;
import  javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;


@FacesConverter(value="dateConverter")
public class CoreDateConverter implements Converter {
	public Object getAsObject(FacesContext context, UIComponent component, String value) {

		return value;
	}

	protected Date convertToDate(FacesContext context, org.primefaces.component.calendar.Calendar pfCalendarComponent, String submittedValue) {
		
		SimpleDateFormat format = new SimpleDateFormat("yyyymmdd");
		format.setTimeZone(pfCalendarComponent.calculateTimeZone());
		
		try {
			return format.parse(submittedValue);
		} catch (ParseException e) {
			throw new ConverterException(e);
		}
	}
	
	public String getAsString(FacesContext context, UIComponent component, Object value) {

		return value.toString();
	}

	protected String convertToString(FacesContext context, org.primefaces.component.calendar.Calendar pfCalendarComponent, Object value) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmdd");

		dateFormat.setTimeZone(pfCalendarComponent.calculateTimeZone());

		return value.toString();
	}
	
}